package com.application.common;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;


/**
 * 
 * @author CHENBINGHAO906
 * @date 2016骞�5鏈�9鏃� 涓婂崍09:18:15
 * @description 闃块噷寮�婧恌astJson宸ュ叿
 * @version 1.0
 */
public class FastJsonMapper {

	/**
	 * 瀵硅薄杞琂son string, 姝tring鏄竴琛屾ā寮�, 濡傛灉闇�瑕佷娇鐢ㄦ牸寮忓寲, 璇蜂娇鐢╰oJsonFormat
	 * 
	 * @param object
	 * @return Json String
	 */
	public static String toJson(Object object) {
		SerializeConfig jsonConfig = new SerializeConfig();
		jsonConfig.put(Date.class, new ValueSerializer());
		jsonConfig.put(Double.class, new ValueSerializer());
		return JSON.toJSONString(object, jsonConfig, new SerializerFeature[] { SerializerFeature.WriteMapNullValue });
	}

	/**
	 * 瀵硅薄杞琂son string, json鍒嗚鏍煎紡
	 * 
	 * @param object
	 * @return Json String
	 */
	public static String toJsonFormat(Object object) {
		SerializeConfig jsonConfig = new SerializeConfig();
		jsonConfig.put(Date.class, new ValueSerializer());
		jsonConfig.put(Double.class, new ValueSerializer());
		return JSON.toJSONString(object, jsonConfig, new SerializerFeature[] { SerializerFeature.WriteMapNullValue,
				SerializerFeature.PrettyFormat, SerializerFeature.WriteNullStringAsEmpty });
	}

	/**
	 * java object杞琂son object
	 * 
	 * @param obj
	 * @return
	 */
	public static Object toJsonObject(Object obj) {
		return JSON.toJSON(obj);
	}

	/**
	 * Json String 杞� Java瀵硅薄
	 * 
	 * @param jsonString
	 * @param clazz
	 * @return
	 */
	public static <T> T fromJson(String jsonString, Class<T> clazz) {
		try {
			if (StringUtils.isEmpty(jsonString)) {
				BaseLogger.writeLog("w", "鎺ユ敹鍒扮┖鎶ユ枃");
				return null;
			}
			return (T) JSON.parseObject(jsonString, clazz);
		} catch (Exception e) {
			BaseLogger.writeLog("e",
					String.format("鎶ユ枃杞崲瀵硅薄鍑洪敊, 璇锋鏌ユ姤鏂囨槸鍚︿负json鏍煎紡, 鎴栬�呮姤鏂囧瓧娈典笌鎵�杞璞″瓧娈垫槸鍚﹀尮閰�.鎶ユ枃:%s, 杞崲瀵硅薄:%s", jsonString, clazz));
			return null;
		}

	}

}

class ValueSerializer implements ObjectSerializer {

	private String format = "yyyy-MM-dd HH:mm:ss";

	public ValueSerializer() {
		super();
	}

	public ValueSerializer(String format) {
		super();
		this.format = format;
	}


	/* (non-Javadoc)
	 * @see com.alibaba.fastjson.serializer.ObjectSerializer#write(com.alibaba.fastjson.serializer.JSONSerializer, java.lang.Object, java.lang.Object, java.lang.reflect.Type)
	 */
	@Override
	public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
		SerializeWriter out = serializer.getWriter();
		if (object instanceof Date) {	
			SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
			out.write(String.format("%s%s%s", "\"", sdf.format(object), "\""));
			return;
		} 
		if (object instanceof Double) {
			DecimalFormat df = new DecimalFormat("#0.00#####");
			out.write(df.format((Double) object));
			return;
		}
		if(object == null){
			out.write("null");
			return;
		}
		out.write(object.toString());
		
	}
}